//**************************************************************
//檔案名稱:ASPMENU20.JS
//目    的:提供GSSCM_ASPMENU20元件所需的JavaScript函數
//
// 版本變更：
// xx. YYYY/MM/DD  AUTHOR    COMMENTS
// 14. 2002/09/18  Roger Lo  修改mnuDropSubMenu(),mnuGetSubMenuLeft(),mnuGetTopPos
// 13. 2002/06/25	 Roger Lo	 修改 mnuHideIfraOverlapAOTObjs()
// 12. 2002/04/17  Roger Lo  判斷IFRAME內的Always-On-Top物件(IE5.5以上)
// 11. 2002/04/10	 Roger Lo	 判斷IE版本決定是否隱藏IFRAME
// 10. 2001/11/30  Roger Lo  修改__grdGetOnTableData()
//**************************************************************

//判斷IE版本 2002/04/10 by Roger Lo
// convert all characters to lowercase to simplify testing
var m_agt=navigator.userAgent.toLowerCase();

// *** BROWSER VERSION ***
// Note: On IE5,these return 4,so use m_is_ie5up to detect IE5.
var m_is_major=parseInt(navigator.appVersion);
var m_is_ie    =((m_agt.indexOf("msie")!=-1) && (m_agt.indexOf("opera")==-1));
var m_is_ie3   =(m_is_ie && (m_is_major < 4));
var m_is_ie4   =(m_is_ie && (m_is_major==4) && (m_agt.indexOf("msie 4")!=-1) );
var m_is_ie4up =(m_is_ie && (m_is_major>=4));
var m_is_ie5   =(m_is_ie && (m_is_major==4) && (m_agt.indexOf("msie 5.0")!=-1) );
var m_is_ie5_5 =(m_is_ie && (m_is_major==4) && (m_agt.indexOf("msie 5.5")!=-1));
var m_is_ie5up =(m_is_ie && !m_is_ie3 && !m_is_ie4);
var m_is_ie5_5up =(m_is_ie && !m_is_ie3 && !m_is_ie4 && !m_is_ie5);


// 共用變數
var gLastTop="";
var gLastDrop="";
var gLastItem="";
var gLastBar="";
var gposLeft=0;
var gposBottom=0;
var gposRight=0;
var gposTop=0;

// 將欲檢查隱藏的物件型態加入矩陣
if (m_is_ie5_5up) {var gAOTTypes=new Array("SELECT","OBJECT","APPLET","EMBED");}
else {var gAOTTypes=new Array("SELECT","IFRAME","OBJECT","APPLET","EMBED");}
//var gAOTTypes=new Array("SELECT","OBJECT","APPLET","EMBED","IFRAME");
var gBakAOTObjs=new Array();
var gBakAOTObjVis=new Array();

var gLastClsMnuTop="";
var gclsMnuTop="";
var gclsMnuTopH="";
var gclsMnuItem="";
var gclsMnuItemH="";
var gclsMnuDisable="";
var gclsMnuDisableH="";
var gblnBackup=false;
var gTrace="";


// 滑鼠移到MenuItem (MENUTYPE=topLabel / menuItem / null) 時
function fnMouseOverMenu(bar)	{
	if(document.readyState!="complete") return false;	
	if (!gblnBackup) mnuBackupAOTObjects();          //將always-on-top物件備份起來 <==不呼叫,20002/04/16 by Roger Lo
	if (gLastBar=='') {
		bar.style.zIndex+=100;      // 將新的Menubar的zIndex屬性增加
		gLastBar=bar;		
	}
	else if (gLastBar!=bar) {    //若為不同的Menubar時
		gLastBar.style.zIndex=bar.style.zIndex;        // 還原之前Menubar的zIndex屬性
		bar.style.zIndex+=100;      // 將新的Menubar的zIndex屬性增加
		gLastBar=bar;
		mnuResetMenu();
		//mnuRecoverAOTObjVis(); -- 在mnuResetMenu()中已經呼叫過了
	}	
	// filter out elements those are not belong to MENUTYPE
	var e=mnuFilterElement(event.toElement);

	// if mouse move out to an element other than a part of menubar ,then quit
	if (e.MENUTYPE== null) return false;
	// 恢復always-on-top物件的顯示狀態
	mnuRecoverAOTObjVis();
	//clean last highlight element
	mnuCleanLastHighlight(e);
	// highlight current topLabel or menuItem and clean last highlight element
	mnuHighlight(e);
	// if sub menu existed,drop down
	mnuDropSubMenu(e);
	// 檢查每個顯示狀態的sub menu是否有干涉到always-on-top objects,若有則將always-on-top objects隱藏
	mnuCleanOnTopObjects();	
}

//將AOT物件以及目前的顯示狀態備份下來
//版本:
// 1.2002/04/16 by Roger Lo,Created
function mnuBackupAOTObjects(){
	var colWin=new Array();
	colWin[0]=document;
	if (m_is_ie5_5up){
		for (var i=0;i<document.frames.length;i++){
			colWin[i+1]=document.frames[i].document;
		}		
	}
	var idx=0;
	for (i=0;i<colWin.length;i++){
		for (var j=0;j<gAOTTypes.length;j++){
			var colAOTObj=colWin[i].all.tags(gAOTTypes[j]);
			for (var k=0;k<colAOTObj.length;k++){
				gBakAOTObjs[idx]=colAOTObj[k];
				gBakAOTObjVis[idx]=colAOTObj[k].style.visibility;
				idx++;
			}			
		}		
	}
	gblnBackup=true;
}


// 滑鼠移出Menubar 時
//修改:
// 1.2002/04/17 by Roger Lo,將gblnBackup旗標切換
function fnMouseOutMenu()	{
	if(document.readyState!="complete") return false;
	var oTo=event.toElement;
	if (!mnuIsPartOfMenu(oTo)) {
		mnuResetMenu();
		gblnBackup=false;
	}
}

// 判斷元素是否屬於Menubar的一部份
function mnuIsPartOfMenu(e)	{
	if (e==null) return false;
	while ( e ) {
		if ((e.MENUTYPE!='topLabel')
		 && (e.MENUTYPE!='menuMain')
		 && (e.MENUTYPE!='menuBar')
		 && (e.MENUTYPE!='subMenu')
		 && (e.MENUTYPE!='disableTopLabel')
		 && (e.MENUTYPE!='verTopLabel')
		 && (e.MENUTYPE!='menuDisableItem')
		 && (e.MENUTYPE!='menuItem'))
			e=e.parentElement;
		else return true;
	}
	return false;
}

// 將Menubar回復到一開始的狀態
function mnuResetMenu()	{
	//window.status='mnuResetMenu() ' ;
	//由最後一個顯示的SubMenu 往上找,直到MenuBar,把已顯示的SubMenu都關起來
	var e=gLastDrop;
	while ((e) && (e!='') && (e.MENUTYPE!='menuMain'))	{
		if (e.MENUTYPE=='subMenu') e.style.display='none';  //若是subMain,則將其隱藏
		e=e.parentElement;
	}
	// 顯示隱藏起來的always-on-top物件
	mnuRecoverAOTObjVis();		
	mnuCleanLastHighlight(gLastItem);
	// 將先前menu item的狀態恢復
	if (gLastItem.MENUTYPE=='menuDisableItem')	gLastItem.className=gclsMnuDisable
	else	gLastItem.className=gclsMnuItem;	
	// 將先前topLabel的狀態恢復
	gLastTop.className=gLastClsMnuTop;		
	gLastDrop='';
	gLastItem='';
	gLastTop='';	
}

//恢復always-on-top物件的顯示狀態
//修改:
// 1.2002/04/16 by Roger Lo,將之前重疊的物件恢復顯示狀態
function mnuRecoverAOTObjVis()   {
	for (var i=0;i<gBakAOTObjs.length;i++) {
		gBakAOTObjs[i].style.visibility=gBakAOTObjVis[i];
	}	
}	


// 將之前的反白處還原並將已放下來的sub menu收起來
// 參數: e: 目前滑鼠所在的Item
function mnuCleanLastHighlight(e)	{			
   // 當滑鼠指到最上層的標題時	
	if (e.MENUTYPE=='topLabel' || e.MENUTYPE=='verTopLabel')	{
		// 第一次移到top label;
		if (gLastTop=='') 	{       
			gLastTop=e;	
			return;
		}		
		// 移到別的top label時
		if (gLastTop!=e)  {        
			gLastTop.className=gclsMnuTop;
			if (gLastDrop!='' && gLastDrop) {	// 將之前打開的sub menu 與其上層的sub menu都關起來    
				gLastDrop.style.display='none';			
				var par=gLastDrop.parentElement;
				while ((par) && (par.MENUTYPE!='menuMain')) {
					if (par.MENUTYPE=='subMenu') par.style.display='none';
					par=par.parentElement;
				}
			}
			gLastTop=e;
		}
		
		//移到同一個top label,但是Last Drop subMenu 不同時,如topLabel1(Drop1) --> menuItem1(Drop2) --> topLabel1(Drop1)
		if (gLastTop==e)	{   
			var sm=mnuHasSubMenu(e);
			if (gLastDrop!=sm && gLastDrop !='' ) gLastDrop.style.display='none';
		}
		
		// 若之前的曾經指到別處,恢復之前的menu item狀態
		if (gLastItem!='') {					
			if (gLastItem.MENUTYPE=='menuDisableItem')	gLastItem.className=gclsMnuDisable
			else	gLastItem.className=gclsMnuItem;						
			gLastItem="";
		}
	} 		
	// 當滑鼠指到非最上層,disabled menu item 時
	if (e.MENUTYPE=='menuDisableItem') {
		if (gLastItem !='' && gLastItem!=e)	
			(gLastItem.MENUTYPE=='menuItem')? gLastItem.className=gclsMnuItem: gLastItem.className=gclsMnuDisable;
		var sm1=mnuBelongToSubMenu(e);          // 找出此menu item所在的sub menu
		if (gLastDrop!=sm1 && gLastDrop!='')	{
			gLastDrop.style.display='none';
			gLastDrop=sm1;
		}
		gLastItem=e;
	}
	
	// 當滑鼠指到非最上層的menu item時
	if (e.MENUTYPE=='menuItem')	{
		if (gLastItem !='' && gLastItem!=e)	
			(gLastItem.MENUTYPE=='menuItem')? gLastItem.className=gclsMnuItem: gLastItem.className=gclsMnuDisable;
		
		var sm=mnuHasSubMenu(e); 		// 找出此menu item所包含的sub menu,若無,則傳回null
		var sm1=mnuBelongToSubMenu(e);          // 找出此menu item所在的sub menu
				
		//if (sm1== null) alert('error: no sm1 in mnuCleanLastHighlight');     // for debug
		//if (sm==sm1) alert('error: sm=sm1 in mnuCleanLastHighlight');     // for debug
		
		// menu item並未包含sub menu,且在同一個subMenu上移動;
		if (sm==null && gLastDrop==sm1)	{;} // do nothing
		
		if (sm==null && gLastDrop!=sm1 && gLastDrop==sm )  	{;} // do nothing	
		
		if (sm==null && gLastDrop!=sm1 && gLastDrop!=sm)	{
			if (gLastDrop && gLastDrop!='') {
				gLastDrop.style.display='none';
				gLastDrop=sm1;
			}
		}		
		if (sm && gLastDrop==sm1)	{;} // do nothing			
		if (sm && gLastDrop!=sm1 && gLastDrop==sm)	{;} // do nothing			
		if (sm && gLastDrop!=sm1 && gLastDrop!=sm) 	{ //若所包含的sub menu與上一個出現的submenu不同時,將上一個收起來
			if (gLastDrop && gLastDrop!='') {
				gLastDrop.style.display='none';       
				gLastDrop=sm;
			}
		}
		gLastItem=e;
	}	
}	


// 找出此menu item所在的sub menu, e.MENUTYPE='menuItem'
function mnuBelongToSubMenu(e) {
	var sm=e.offsetParent;
	while (sm.MENUTYPE!='subMenu')	{
		sm=sm.offsetParent;
	}
	return sm;	
}

// 找出是否包含Sub Menu,若有則放下來
//修改:
// 1.2002/09/18 by Roger Lo,SubMenu的Top都要重算(防止Scroll)
function mnuDropSubMenu(e)	{
	var sm=mnuHasSubMenu(e);
	if (sm) 	{
		//計算sub menu的位置與大小
		if (e.MENUTYPE=='topLabel')	{
			sm.style.display='block';         //先顯示後offsetWidth(唯讀)才抓得到;但pixelWidth仍為0
			sm.style.pixelTop=mnuGetTopPos(e,sm);
			if (sm.style.pixelWidth==0) sm.style.pixelWidth=sm.rows[0].offsetWidth+50;
			sm.style.pixelLeft=mnuGetSubMenuLeft(e,sm);  //如果submenu太右邊時,修正sub menu的位置
		}		
		else if (e.MENUTYPE=='verTopLabel')	{
			sm.style.display='block';         //先顯示後offsetWidth(唯讀)才抓得到;但pixelWidth仍為0
			sm.style.pixelTop=mnuGetTopPos(e,sm);
			if (sm.style.pixelWidth==0)   sm.style.pixelWidth=sm.rows[0].offsetWidth+50;
			if (sm.style.pixelLeft==0) sm.style.pixelLeft=e.offsetLeft+e.offsetWidth -5;
		}			
		else if (e.MENUTYPE=='menuItem')	{
			sm.style.display='block';
			sm.style.pixelTop=mnuGetTopPos(e,sm);
			if (sm.style.pixelWidth==0) sm.style.pixelWidth=sm.rows[0].offsetWidth+50;
			if (sm.style.pixelLeft==0) sm.style.pixelLeft=mnuGetSubMenuLeft(e,sm);					
		}
		gLastDrop=sm;
	}		
}	

//檢查每個顯示狀態的sub menu是否有干涉到always-on-top objects,若有則將always-on-top objects隱藏
//修改:
// 1.2002/04/16 by Roger Lo,用LastBar去找SubMenu縮小範圍
function mnuCleanOnTopObjects()	{
	//alert(gLastBar.id);
	var tables=gLastBar.all.tags('TABLE');
	for (var i=0; i<tables.length; i++)	{
		if (tables[i].MENUTYPE=='subMenu' && 
	    tables[i].style.display=='block' ) {
	    	mnuHideOverlapAOTObjs(tables[i]);
		}
	}
}


// 計算menu item 相對應的sub menu的高度位置
//修改
// 1.2002/09/18 by Roger Lo,SubMenu太長超過螢幕時,要拉上來
function mnuGetTopPos(e,sm)	{
		var iBdH=document.body.clientHeight;
		var iScT=document.body.scrollTop;
		var iItmT=e.offsetTop;
		var iSmH=sm.offsetHeight;
		var par=e.offsetParent;
		while (par){
			iItmT += par.offsetTop;
			par=par.offsetParent;
		}		

		var posTop=e.offsetTop;
		var par=e.offsetParent;		
		if (e.MENUTYPE=='menuItem'){
			while (par.MENUTYPE!='subMenu')	{			
				posTop += par.offsetTop;
				par=par.offsetParent;
			}
		} else if (e.MENUTYPE=='topLabel'){posTop=e.offsetTop+e.offsetHeight+2}
			else {posTop=e.offsetTop+2}

		if ((iItmT+iSmH)>(iBdH+iScT)){
			var iRtn=(posTop+(iBdH+iScT)-(iItmT+iSmH));
			if (e.MENUTYPE=='topLabel'){iRtn=iRtn-e.offsetHeight-2}
			return iRtn;
		} else return posTop;
}


// 取得sub menu相對於BODY的位置
function mnuGetSubMenuPos(sm)	{
	var posLeft=sm.offsetLeft;
	var posTop=sm.offsetTop;
	var oParent=sm.offsetParent;	
	while (oParent.tagName!='BODY')	{
		posLeft+=oParent.offsetLeft;
		posTop+=oParent.offsetTop;
		oParent=oParent.offsetParent;
	}
	gposLeft=posLeft;
	gposBottom=posTop+sm.offsetHeight;
	gposRight=posLeft+sm.offsetWidth;
	gposTop=posTop;
}


//檢查AOT物件是否與SubMenu重疊
//修改:
//	1.2002/04/16 by Roger Lo,Created
function mnuIsAOTObjOverlap(oAOT,iParentLeft,iParentTop){
	//alert('mnuIsAOTObjOverlap');
	var iLeft=0,iRight=0,iTop=0,iBottom=0
	
	// 計算該物件目前相對於BODY的位置		
	iLeft=oAOT.offsetLeft+iParentLeft;
	iTop=oAOT.offsetTop+iParentTop;
	//alert('0 oAOT.id='+oAOT.id+',Top:'+iTop+',Bottom:'+iBottom+',Right:'+iRight+',Left:'+iLeft);
	var oParent=oAOT.offsetParent;
	while (oParent.tagName!='BODY')	{
		//alert('a - '+oParent.tagName);
		iLeft += oParent.offsetLeft;
		iTop += oParent.offsetTop;
		//alert('1.x oAOT.id='+oAOT.id+',Top:'+iTop+',Bottom:'+iBottom+',Right:'+iRight+',Left:'+iLeft);
		oParent=oParent.offsetParent;
		//alert('b - '+oParent.tagName);
	}
	iBottom=iTop+oAOT.offsetHeight;	
	iRight=iLeft+oAOT.offsetWidth;
	//alert('1 oAOT.id='+oAOT.id+',Top:'+iTop+',Bottom:'+iBottom+',Right:'+iRight+',Left:'+iLeft);
	//檢查是否重疊
	// 若物件低於sub menu的下緣,高於sub menu上緣時,
	// 或物件右邊小於sub menu左邊,物件左邊大於submenu右邊,則不會重疊
	if (iTop>=gposBottom || iBottom<=gposTop || iRight<=gposLeft || iLeft>=gposRight) {
		return false;
	} else {return true}
}


// 隱藏在SubMenu範圍內的 IFrame,Combo,Embeded Object
//修改:
//	2002/04/16 by Roger Lo,當IE5.5時,考慮IFRAME內的Always-On-Top物件
function mnuHideOverlapAOTObjs(sm){
	mnuGetSubMenuPos(sm);
	var i=0,j=0
	for (i=0;i<gAOTTypes.length;i++){
		var colAOTObj=document.all.tags(gAOTTypes[i]);
		for (j=0;j<colAOTObj.length;j++){
			if (mnuIsAOTObjOverlap(colAOTObj[j],0,0)) {
				colAOTObj[j].style.visibility='hidden';
			}
		}
	}	
	//若為ie5.5以上,則考慮iframe內的情形
	if (m_is_ie5_5up) {mnuHideIfraOverlapAOTObjs(sm);}
}	


//當ie5.5以上時,隱藏iframe內容中重疊的AOT物件
//修改
// 2.2002/06/25 by Roger Lo, 檢查iframe內文件時,要避開iframe的網頁尚未載入的空檔
// 1.2002/04/16 by Roger Lo, Created
function mnuHideIfraOverlapAOTObjs(sm){
	var colIframe=document.all.tags('IFRAME');
	for (var i=0;i<document.frames.length;i++){		
		oIframe=colIframe[i];
		var iIfraLeft=oIframe.offsetLeft;
		var iIfraTop=oIframe.offsetTop;		
		var obj=oIframe.offsetParent;
		while (obj.tagName!='BODY'){
			iIfraLeft+=obj.offsetLeft;
			iIfraTop+=obj.offsetTop;
			obj=obj.offsetParent;
		}
		var oIfraWin=document.frames[i];
		for (var j=0;j<gAOTTypes.length;j++){
			if (oIfraWin.document.body!=null){
				var colAOTObj=oIfraWin.document.all.tags(gAOTTypes[j]);
				var ifrScrTop=oIfraWin.document.body.scrollTop;
				var ifrScrLeft=oIfraWin.document.body.scrollLeft;				
				for (var x=0;x<colAOTObj.length;x++){
					if (mnuIsAOTObjOverlap(colAOTObj[x],iIfraLeft-ifrScrLeft,iIfraTop-ifrScrTop)) {
						colAOTObj[x].style.visibility='hidden';
					}
				}
			}
		}
	}
}


// 傳回下放的sub menu的左座標
//修改:
//1.2002/09/18 by Roger Lo,如果SubMenu蓋到topLabel時要拉開
function mnuGetSubMenuLeft(e,sm)	{
	var pos
	var del
	var smRight
	var margin=document.body.clientWidth  //- document.body.borderwidth*2 - document.body.paddingwidth*2 - document.body.marginwidth*2; 

	if (e.MENUTYPE=='topLabel') {
	    //if (sm.style.pixelLeft==0) sm.style.pixelLeft=e.offsetLeft+2;
	    	sm.style.pixelLeft=e.offsetLeft+2;
    		smRight=e.offsetLeft;
    		var par=e.offsetParent;
    		while (par.tagName!='BODY')	{smRight += par.offsetLeft; par=par.offsetParent;}
    		smRight += sm.style.pixelWidth;
		del=smRight - margin;
		pos=(del>0)?( sm.style.pixelLeft - del - 5 ) : sm.style.pixelLeft;
		if ((e.offsetTop+e.offsetHeight)>sm.offsetTop){
			if ((e.offsetLeft+e.offsetWidth)>sm.offsetLeft){
				pos=pos+e.offsetWidth
			}
		}

	}
	if (e.MENUTYPE=='menuItem')	{
		if (sm.style.pixelLeft==0) sm.style.pixelLeft=mnuGetOffsetLeft(e)+e.offsetWidth - 5;
		smRight=mnuGetSubMenuRealRight(sm);
		pos=(smRight>margin)? (e.offsetLeft - sm.style.pixelWidth) : sm.style.pixelLeft;
	}
	return pos;	
}


// 傳回sub menu相對於body border的右座標
function mnuGetSubMenuRealRight(sm) {
	var left=sm.offsetLeft;
	var par=sm.offsetParent;
	while (par.tagName!='BODY')	{
		left += par.offsetLeft;
		par=par.offsetParent;
	}
	var right=left+sm.offsetWidth;
	return right;
}

//取得物件的Left座標
function mnuGetOffsetLeft(e)	{
	var left=e.offsetLeft ;
	while (e.MENUTYPE!='subMenu')	{
		left +=  e.offsetLeft;
		e=e.offsetParent;
	}
	return left;
}

// 傳回所包含的Sub Menu,若無,則傳回null
function mnuHasSubMenu(e)	{
	var ch=e.children.tags("TABLE");
	if (ch.length==0 ) return null;
	var i=0;
	while (i<ch.length)	{
		if (ch[i].MENUTYPE=='subMenu') return ch[i];
		i++;
	}
}	

// 將標題反白,並將之前狀態回復
function mnuHighlight(e)	{
	//window.status='mnuHighlight - '+e.tagName+' - '+e.MENUTYPE+' - '+e.id;
	
	// when mouse over Top label
	if (e.MENUTYPE=='topLabel'  || e.MENUTYPE=='verTopLabel')	{
		e.className=gclsMnuTopH;
		if (gLastClsMnuTop!=gclsMnuTop) gLastClsMnuTop= gclsMnuTop;
		return;
	}		
	// when mouse over Disabled Top label
	if (e.MENUTYPE=='disableTopLabel')	{
		mnuResetMenu();
		return;
	}		
	// when mouse over menuitem
	if (e.MENUTYPE=='menuItem')	{
		e.className=gclsMnuItemH;
		return;
	}	
	// when mouse over disabled menu item
	if (e.MENUTYPE=='menuDisableItem')	{
		e.className=gclsMnuDisableH;
		return;
	}
}


// 過濾事件的元素 ,只處理
// MENUTYPE=MENUTYPE=topLabel / menuItem / subMenu / null 的元素 
// 修改:
//	2001/11/30 by Roger Lo
function mnuFilterElement(e)	{
	var temp=e;
	while  ((temp)
		 && (temp.MENUTYPE!='menuMain') 
		 && (temp.MENUTYPE!='menuBar') 
		 && (temp.MENUTYPE!='topLabel')
		 && (temp.MENUTYPE!='verTopLabel')	 
		 && (temp.MENUTYPE!='subMenu')
		 && (temp.MENUTYPE!='disableTopLabel')
		 && (temp.MENUTYPE!='menuDisableItem')
		 && (temp.MENUTYPE!='menuItem'))		{	
			e=temp ;
			temp=e.parentElement;			
	}
	return temp;
}


// 設定topLabel,menuItem的Class Name(正常/反白)
function fnSetMenuClass( c1,c2,c3,c4,c5,c6)	{
	gclsMnuTop=c1;
	gclsMnuTopH=c2;
	gclsMnuItem=c3;
	gclsMnuItemH=c4;
	gclsMnuDisable=c5;
	gclsMnuDisableH=c6;
}	   